package com.jjc.MyWebApp.Core;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.*;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.SqlReporter;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.template.Engine;
import com.jjc.MyWebApp.Constant.ConfigConstant;
import com.jjc.MyWebApp.Constant.I18nConstant;
import com.jjc.MyWebApp.Interceptor.AuthInterceptor;
import com.jjc.MyWebApp.Interceptor.i18nInterceptor;
import com.jjc.MyWebApp.Model._MappingKit;
import com.jjc.MyWebApp.Plugin.ServicePlugin;
import com.jjc.MyWebApp.Routes.RoutesScan;
import com.jjc.MyWebApp.directive.currentUser;
import com.jjc.MyWebApp.kit.StringKit;


/**
 * Created by jiaji on 2017/4/26.
 */
public class WebAppConfig extends JFinalConfig {

    /**
     * 日志对象
     */
    private static final Log log = Log.getLog(WebAppConfig.class);

    public void configConstant(Constants constants) {
        if (log.isInfoEnabled()) log.info("Config 缓存配置文件");
        PropKit.use("config");

        if (log.isInfoEnabled()) log.info("Config: 设置字符集");
        constants.setEncoding(StringKit.encoding);

        if (log.isInfoEnabled()) log.info("Config: 设置是否开发模式");
        constants.setDevMode(PropKit.getBoolean(ConfigConstant.config_devMode, false));

//        if(log.isInfoEnabled()) log.info("configConstant 视图error page设置");
//        constants.setError404View("/platform/common/404.html");
//        constants.setError500View("/platform/common/500.html");

        if (log.isInfoEnabled()) log.info("Config: i18n文件前缀设置");
        constants.setI18nDefaultBaseName(I18nConstant.I18n_Base_Name);

        if (log.isInfoEnabled()) log.info("Config: 把预处理sql打印到Log，而不是控制台输出");
        if (constants.getDevMode()) {
            SqlReporter.setLog(true);
        }

    }

    public void configRoute(Routes routes) {
        if (log.isInfoEnabled()) log.info("Config: 注册路由");
        routes.add(new RoutesScan());
    }

    /**
     * 加载Jfinal template Engine公共模版
     * @param engine 由Jfinal调用
     */
    public void configEngine(final Engine engine) {
        if (log.isInfoEnabled()) log.info("Config: 加载模版");
        engine.addSharedFunction("/view/Common/Layout.html");
        engine.addSharedFunction("/view/Common/util.html");
        if(log.isInfoEnabled()) log.info("Config: 加载自定义指令");
        engine.addDirective("user",new currentUser());
    }

    public void configPlugin(Plugins plugins) {
        if (log.isInfoEnabled()) log.info("Config: 配置数据缓存插件");
        EhCachePlugin ehcache = new EhCachePlugin();

        if (log.isInfoEnabled()) log.info("Config: 配置Druid数据库连接池");
        DruidPlugin Druid = new DruidPlugin(
                PropKit.get(ConfigConstant.config_mysql_jdbcUrl),
                PropKit.get(ConfigConstant.config_mysql_userName),
                PropKit.get(ConfigConstant.config_mysql_passWord),
                PropKit.get(ConfigConstant.config_mysql_driver));
        Druid.set(
                PropKit.getInt(ConfigConstant.config_druid_initialSize),
                PropKit.getInt(ConfigConstant.config_druid_minIdle),
                PropKit.getInt(ConfigConstant.config_druid_maxActive));
        Druid.addFilter(new StatFilter());
        WallFilter wall = new WallFilter();
        wall.setDbType("mysql");
        WallConfig config = new WallConfig();
        config.setFunctionCheck(false); // 支持数据库函数
        wall.setConfig(config);
        Druid.addFilter(wall);

        if (log.isInfoEnabled()) log.info("Config: 配置ActiveRecordPlugin插件");
        ActiveRecordPlugin ActiveRecord = new ActiveRecordPlugin(Druid);
        ActiveRecord.setDevMode(PropKit.getBoolean(ConfigConstant.config_devMode, false));
        ActiveRecord.setShowSql(PropKit.getBoolean(ConfigConstant.config_devMode, false));
        ActiveRecord.setContainerFactory(new CaseInsensitiveContainerFactory(true));//设置大小写不敏感
        ActiveRecord.setBaseSqlTemplatePath(PathKit.getRootClassPath() + "/sql");
        ActiveRecord.addSqlTemplate("All.sql");
        ActiveRecord.setDialect(new MysqlDialect());
        _MappingKit.mapping(ActiveRecord);

        if (log.isInfoEnabled()) log.info("Config: 注册插件");
        plugins.add(ehcache);               //缓存插件
        plugins.add(Druid);                 //数据库连接池插件
        plugins.add(ActiveRecord);          //数据库操作插件
        plugins.add(new ServicePlugin());   //自动注入Service
    }

    public void configInterceptor(Interceptors interceptors) {
        interceptors.add(new i18nInterceptor());
        interceptors.add(new AuthInterceptor());

    }

    public void configHandler(Handlers handlers) {

    }
}
